package com.qx.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.qx.frame.MainFrame;
import com.qx.iconst.PathConst;

/**
 * 描述：<br>
 * 图形组件工具类
 * 
 * @author LazyBear
 * @data 2014-4-22
 */
public final class SwingUtils {

	/**
	 * 初始化窗体
	 * 
	 * @author LazyBear 描述:<br>
	 */
	public static void initFrame(JFrame frame) {
		try {
			Properties pro = new Properties();
			InputStream in = new BufferedInputStream(new FileInputStream(PathConst.mainFrame_cfg));
			pro.load(in);
			frame.setDefaultCloseOperation(Integer.valueOf(pro.getProperty("operation")));
			MainFrame.setDefaultLookAndFeelDecorated(Boolean.valueOf(pro.getProperty("defaultLookAndFeelDecorated")));
			frame.setLayout(null);
			frame.setResizable(Boolean.valueOf(pro.getProperty("resizable")));
			frame.setSize(Integer.valueOf(pro.getProperty("width")), Integer.valueOf(pro.getProperty("hight")));
			frame.setTitle(pro.getProperty("title"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @author LazyBear 描述:<br>
	 * @param panel
	 */
	public static void initPanel(JPanel panel) {
		try {
			Properties pro = new Properties();
			InputStream in = new BufferedInputStream(new FileInputStream(PathConst.Panel_cfg));
			pro.load(in);
			panel.setBounds(Integer.valueOf(pro.getProperty("init_x")), Integer.valueOf(pro.getProperty("init_y")),
					Integer.valueOf(pro.getProperty("width")), Integer.valueOf(pro.getProperty("hight")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @author LazyBear 描述:<br>
	 *         窗口屏幕居中
	 */
	public static void setMidScree(JFrame frame) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
		frame.setVisible(true);
	}
}
